import 'package:fish_redux/fish_redux.dart';
import '../model/order_view_model.dart';
import 'dart:convert' as convert;
import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';

Reducer<SafeOrderViewState> buildReducer() {
  return asReducer(
    <Object, Reducer<SafeOrderViewState>>{
      SafeOrderViewAction.action: _onAction,
      SafeOrderViewAction.onGetData: _onGetData,
    },
  );
}

SafeOrderViewState _onAction(SafeOrderViewState state, Action action) {
  final SafeOrderViewState newState = state.clone();
  return newState;
}

SafeOrderViewState _onGetData(SafeOrderViewState state, Action action) {
  final SafeOrderViewState newState = state.clone();
  if (action.payload == null) {
    newState.isEmpty = true;
  } else {
    Map data = action.payload;
    newState.isEmpty = false;
    newState.orderStatus = data['CheckOrderResultStatus'];
    newState.lastTime = CommonUtils.checkNullToString(data['CheckorderAdddate']);
    String orderPhoto =  CommonUtils.checkNullToString(data['CheckorderPhoto']);
    newState.orderPhoto = orderPhoto.split(',');
    newState.orderSignPhoto = CommonUtils.checkNullToString(data['CheckOrderSign']);
    List list = convert.jsonDecode(CommonUtils.checkNullToString(data['CheckOrderQuestionStr']));
    List<OrderConfigModel> items = list
        .map<OrderConfigModel>((e) => OrderConfigModel.fromJson(e))
        .toList();
    newState.items = items;
  }
  newState.isLoad = true;
  return newState;
}
